
import EventEmitter from "events";
class Util extends EventEmitter {

    constructor() {
        super();
        this.loop(0);
    }

    loop(tick) {

        this.emit("loop", tick);

        requestAnimationFrame(tick => this.loop(tick));

    }

    canvasToDataURL(canvas) {
        return canvas.toDataURL();
    }

    dataURLToBlob(dataurl) {
        var arr = dataurl.split(',');
        var mime = arr[0].match(/:(.*?);/)[1];
        var bstr = atob(arr[1]);
        var n = bstr.length;
        var u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return URL.createObjectURL(new Blob([u8arr], { type: mime }));
    }

    canvasToBlob(canvas) {
        return this.dataURLToBlob(this.canvasToDataURL(canvas));
    }
}

export default new Util;